package gui;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import db.Person;

@SuppressWarnings("serial")
public class SucheKunde extends JPanel {
  
 
  private JPanel pane = new JPanel();
  
  private boolean tabelleVorhanden; 
  private JTextField name;
  private JButton suche;
  private JScrollPane teppich;
  private JTextField vorname;
  private JTextField kundenNr;
  private JTextField ort;
  private JLabel lblVorname;
  private JLabel lblKundenNr;
  private JLabel lblOrt;
  private Person searchedCustomer=null;
    
  public SucheKunde() {
    
    tabelleVorhanden = false;

    pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
        
        suche = new JButton("Suche");
        
        name = new JTextField(10);
        
        vorname = new JTextField();
        vorname.setColumns(10);
        
        kundenNr = new JTextField();
        kundenNr.setColumns(10);
        
        ort = new JTextField();
        ort.setColumns(10);
        
        JLabel lblName = new JLabel("Name");
        
        lblVorname = new JLabel("Vorname");
        
        lblKundenNr = new JLabel("Kunden Nr");
        
        lblOrt = new JLabel("Ort");
        GroupLayout groupLayout = new GroupLayout(this);
        groupLayout.setHorizontalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addComponent(pane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addGroup(groupLayout.createSequentialGroup()
        					.addContainerGap()
        					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
        						.addComponent(lblName, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
        						.addComponent(lblVorname, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
        						.addComponent(lblKundenNr, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
        						.addComponent(lblOrt, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        						.addComponent(ort, 318, 318, Short.MAX_VALUE)
        						.addComponent(kundenNr, 318, 318, Short.MAX_VALUE)
        						.addComponent(name, GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE)
        						.addComponent(vorname, GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE)))
        				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
        					.addContainerGap(379, Short.MAX_VALUE)
        					.addComponent(suche)))
        			.addContainerGap())
        );
        groupLayout.setVerticalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addComponent(pane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblName, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addComponent(lblVorname, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
        				.addComponent(vorname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(kundenNr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblKundenNr, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(ort, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblOrt, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addComponent(suche)
        			.addContainerGap(155, Short.MAX_VALUE))
        );
        setLayout(groupLayout);
        
        suche.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	String custemerID=kundenNr.getText();
          		if(custemerID!=null && custemerID.length()>0){
          			searchedCustomer= new Person();
      			    searchedCustomer.setId(Integer.parseInt(kundenNr.getText()));
      			    searchedCustomer.setOrt(ort.getText());
      			    searchedCustomer.setName(name.getText());
      			    searchedCustomer.setVorname(vorname.getText());
          			searchedCustomer=searchedCustomer.searchCustomer();
              		 if(searchedCustomer!=null){
              			 if (tabelleVorhanden == true) {
                             zweiterAufruf();
                           } 
                           else {
                               ersterAufruf();
                           }
                 		}else{
                  			Dialog.nichtVorhanden();
                 		 }
          		} else{
                 			Dialog.kundenNrMandatory();
                 		}
              
              	 
          		
            }
          });

  }
  /* create table and fill table */
  void ersterAufruf()  {
    teppich=new JScrollPane(new JScrollPane(new SuchErgebniss(searchedCustomer)), JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    
    pane.add(teppich);
    revalidate();
      
    tabelleVorhanden = true;
    
  }
  /* update table */
  public void zweiterAufruf() {

	  pane.remove(teppich);
	  
	 /* ScrollPane scrollable */
	  
	  teppich=(new JScrollPane(new JScrollPane(new SuchErgebniss(searchedCustomer)), JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
	                                                   JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
	  
	  pane.add(teppich);
	
	  revalidate();
    } 
}